//
//  ChoooseGiftCollectionViewController.m
//  ChooseGift
//
//  Created by lanou on 15/12/16.
//  Copyright (c) 2015年 lanou. All rights reserved.
//
#define kwidth [UIScreen mainScreen].bounds.size.width

#import "ChoooseGiftCollectionViewController.h"
#import "ChooseGift.h"
#import "ChooseGiftCollectionViewCell.h"
#import "PopoverView.h"
#import "WebViewController.h"
#import "MJRefresh.h"
@interface ChoooseGiftCollectionViewController ()
@property (nonatomic,strong) NSMutableArray *modelArray;

@property (nonatomic,strong) NSMutableArray *idArray;



@property (nonatomic,strong) NSArray *personalityArray;
@property (nonatomic,strong) NSArray *targetArray;
@property (nonatomic,strong) NSArray *sceneArray;
@property (nonatomic,strong) NSArray *priceArray;

@property (nonatomic,assign) NSString *personality;
@property (nonatomic,assign) NSString *target;
@property (nonatomic,assign) NSString *scene;
@property (nonatomic,assign) NSString *price;

@property (nonatomic,assign) NSString *sort;


@end

static NSInteger num = 0;



@implementation ChoooseGiftCollectionViewController

- (NSMutableArray *)modelArray{
    if (!_modelArray) {
        _modelArray = [[NSMutableArray alloc]init];
        
    }
    return _modelArray;
}

- (NSMutableArray *)idArray{
    if (!_idArray) {
        _idArray = [[NSMutableArray alloc]init];
    }
    return _idArray;
}



static NSString * const reuseIdentifier = @"Cell";
- (void)setData{
    
    NSString *string1 = [NSString stringWithFormat:@"&target=%@",self.target];
    NSString *string2 = [NSString stringWithFormat:@"&personality=%@",self.personality];
    NSString *string3 = [NSString stringWithFormat:@"&price=%@",self.price];
    NSString *string4 = [NSString stringWithFormat:@"&scene=%@",self.scene];
     NSString *string5 = [NSString stringWithFormat:@"&sort=%@",self.sort];
    if (self.target == nil) {
        string1 = @"";
    }
    if (self.personality == nil) {
        string2 = @"";
    }
    if (self.price == nil) {
        string3 = @"";
    }
    if (self.scene == nil) {
        string4 = @"";
    }
    if (self.sort == nil) {
        string5 = @"";
    }
    NSString *string = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/search/item_by_type?limit=20&offset=%ld%@%@%@%@%@",(long)num,string2,string3,string4,string5,string1];
    NSLog(@"%@",string);
//    NSURL *url = [NSURL URLWithString:string];
//    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
//    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        NSDictionary *rootvc = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//        NSArray *array = rootvc[@"data"][@"items"];
//        for (NSDictionary *dic in array) {
//            ChooseGift *choose = [[ChooseGift alloc]init];
//            [choose setValuesForKeysWithDictionary:dic];
//            [self.idArray addObject:choose.ID];
//            [self.modelArray addObject:choose];
//            
//        }
//        
//        [self.collectionView reloadData];
//        
//    }];
    
    [LORequestManger GET:string success:^(id response) {
        NSDictionary *rootvc = (NSDictionary *)response;
        NSArray *array = rootvc[@"data"][@"items"];
        for (NSDictionary *dic in array) {
            ChooseGift *choose = [[ChooseGift alloc]init];
            [choose setValuesForKeysWithDictionary:dic];
            [self.idArray addObject:choose.ID];
            [self.modelArray addObject:choose];
            
        }
        
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setData];
    self.title = @"选礼";
    

   
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemCompose) target:self action:@selector(barbutton:)];
    
    
//    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.collectionView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    self.personalityArray = [[NSArray alloc]init];
    self.targetArray = [[NSArray alloc]init];
    self.sceneArray = [[NSArray alloc]init];
    self.priceArray = [[NSArray alloc]init];
    
    self.personalityArray = @[@"26",@"28",@"43",@"44",@"45",@"46",@"47"];
    self.targetArray = @[@"16",@"17",@"18",@"19",@"20",@"21",@"22"];
    self.sceneArray = @[@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37"];
    self.priceArray = @[@"0_50",@"50_200",@"200_500",@"500_1000",@"1000_100000"];
    
    
    
    __weak ChoooseGiftCollectionViewController *mainTableView = self;
    [self.collectionView addFooterWithCallback:^{
        num += 20;
        [mainTableView setData];
        [mainTableView.collectionView reloadData];


        [mainTableView.collectionView footerEndRefreshing];
    }];

    __weak ChoooseGiftCollectionViewController *mainTableView1 = self;
    [self.collectionView addHeaderWithCallback:^{
        _modelArray = [[NSMutableArray alloc]init];
        [mainTableView1 setData];
        [mainTableView1.collectionView reloadData];
        
        
        [mainTableView1.collectionView headerEndRefreshing];
    }];
    

    
    
    
    
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.minimumInteritemSpacing = 10;
    flowlayout.minimumLineSpacing = 10;
    flowlayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowlayout.itemSize = CGSizeMake(kWidth / 2 - 20, 0.4 * kHeight);
    
    
    self.collectionView.collectionViewLayout = flowlayout;
    flowlayout.headerReferenceSize = CGSizeMake(0, 40);
    
    
    
    [self.collectionView registerClass:[ChooseGiftCollectionViewCell class] forCellWithReuseIdentifier:@"HP"];
    [self.view addSubview:self.collectionView];
    
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header1"];
   

    
    
    
    
    
    
    UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setFrame:CGRectMake(0, 64,kwidth / 4 , 40)];
    [button1 setBackgroundColor:[UIColor whiteColor]];
    [button1 setTitle:@"对象" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [button1.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [button1 addTarget:self action:@selector(button1Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    
    
    UIButton *button2=[UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setFrame:CGRectMake(kwidth / 4, 64,kwidth / 4 , 40)];
    [button2 setBackgroundColor:[UIColor whiteColor]];
    [button2 setTitle:@"场合" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [button2.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [button2 addTarget:self action:@selector(button2Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    UIButton *button3=[UIButton buttonWithType:UIButtonTypeCustom];
    [button3 setFrame:CGRectMake(kwidth / 2 - 5, 64,kwidth / 4 , 40)];
    [button3 setBackgroundColor:[UIColor whiteColor]];
    [button3 setTitle:@"个性" forState:UIControlStateNormal];
    [button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button3 setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [button3.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [button3 addTarget:self action:@selector(button3Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
    
    
    UIButton *button4=[UIButton buttonWithType:UIButtonTypeCustom];
    [button4 setFrame:CGRectMake(kwidth * 3 / 4 - 10,64,kwidth / 4 + 10 , 40)];
    [button4 setBackgroundColor:[UIColor whiteColor]];
    [button4 setTitle:@"价格" forState:UIControlStateNormal];
    [button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button4 setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [button4.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [button4 addTarget:self action:@selector(button4Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button4];

   }

- (void)barbutton:(UIButton *)sender{
    CGPoint point = CGPointMake(kWidth - kWidth / 10 ,50);
    NSArray *titles = @[@"默认排序", @"按热度排序", @"价格从低到高",@"价格从高到低"];
       PopoverView *pop = [[PopoverView alloc] initWithPoint:point titles:titles images:nil];
    pop.selectRowAtIndex = ^(NSInteger index){
        
        if (index == 0) {
            self.modelArray = [NSMutableArray array];
            self.idArray = [NSMutableArray array];
            self.sort = nil;
            [self setData];
            [self.collectionView reloadData];
        
        }
        if (index == 1) {
            self.modelArray = [NSMutableArray array];
            self.idArray = [NSMutableArray array];
            self.sort = @"hot";
            [self setData];
            [self.collectionView reloadData];
            
        }
        if (index == 2) {
            self.modelArray = [NSMutableArray array];
            self.idArray = [NSMutableArray array];
            self.sort = @"price%3Aasc";
            [self setData];
            [self.collectionView reloadData];
            
            
        }
        if (index == 3) {
            self.modelArray = [NSMutableArray array];
            self.idArray = [NSMutableArray array];
        self.sort = @"price%3Adesc";
            [self setData];
            [self.collectionView reloadData];

        }
        
        
        
        
        
    };
    [pop show];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    ChooseGiftCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HP" forIndexPath:indexPath];
    ChooseGift *choose = self.modelArray[indexPath.row];
    cell.choose = self.modelArray[indexPath.row];
    cell.choose = choose;
    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
}



#pragma mark------挑选部分------


-(void)button1Clicked:(UIButton *)sender
{
    
    CGPoint point = CGPointMake(sender.frame.origin.x + sender.frame.size.width/2, sender.frame.origin.y + sender.frame.size.height);
    NSArray *titles = @[@"全部", @"男票", @"女盆友",@"闺蜜们",@"基友",@"爸爸妈妈",@"小盆友",@"同事"];
    NSArray *images = @[@"1182103.gif", @"bt11.gif", @"bt12.gif", @"bt13.png", @"bt14.gif", @"bt15.gif", @"bt16.gif", @"bt17.gif"];
    
    PopoverView *pop = [[PopoverView alloc] initWithPoint:point titles:titles images:images];
    pop.selectRowAtIndex = ^(NSInteger index){
        [sender setTitle:titles[index] forState:(UIControlStateNormal)];
        
        if (index == 0) {
            self.modelArray = [NSMutableArray array];
            self.idArray = [NSMutableArray array];
            [sender setTitle:@"对象" forState:(UIControlStateNormal)];
            
            self.target = nil;
            [self setData];
            [self.collectionView reloadData];
        }else{
            self.modelArray = [NSMutableArray array];
            self.idArray = [NSMutableArray array];
            self.target = self.targetArray[index - 1];
            NSLog(@"%@",self.target);
            
            [self setData];
            [self.collectionView reloadData];
            
            
            
        }
        
        
    };
    [pop show];
}
-(void)button2Clicked:(UIButton *)sender
{
    
    
    CGPoint point = CGPointMake(sender.frame.origin.x + sender.frame.size.width/2, sender.frame.origin.y + sender.frame.size.height );
    NSArray *titles = @[@"全部", @"生日", @"情人节",@"结婚",@"新年",@"感谢",@"纪念日",@"乔迁",@"圣诞节"];
     NSArray *images = @[@"1139967.gif", @"3.gif", @"4.gif", @"1.gif", @"7.gif", @"8.gif", @"5.gif", @"2.gif", @"6.gif"];
    
    PopoverView *pop = [[PopoverView alloc] initWithPoint:point titles:titles images:images];
    pop.selectRowAtIndex = ^(NSInteger index){
        NSLog(@"select index:%ld", (long)index);
        [sender setTitle:titles[index] forState:(UIControlStateNormal)];
        if (index == 0) {
            self.modelArray = [NSMutableArray array];
            self.idArray = [NSMutableArray array];
            [sender setTitle:@"场合" forState:(UIControlStateNormal)];
            self.scene = nil;
            [self setData];
            [self.collectionView reloadData];
            
        }else{
            self.modelArray = [NSMutableArray array];
            self.idArray = [NSMutableArray array];
            self.scene = self.sceneArray[index - 1];
            [self setData];
            [self.collectionView reloadData];
            
            
        }
    };
    [pop show];
}




-(void)button3Clicked:(UIButton *)sender
{
    
    CGPoint point = CGPointMake(sender.frame.origin.x + sender.frame.size.width/2, sender.frame.origin.y + sender.frame.size.height );
    NSArray *titles = @[@"全部", @"萌", @"小清新",@"创意",@"奇葩",@"文艺范",@"科技感",@"设计感"];
    NSArray *images = @[@"bt31.gif", @"bt32.gif", @"bt33.gif", @"bt34.gif", @"bt35.gif", @"bt36.gif", @"bt37.gif", @"bt38.gif"];
    
    PopoverView *pop = [[PopoverView alloc] initWithPoint:point titles:titles images:images];
    pop.selectRowAtIndex = ^(NSInteger index){
        NSLog(@"select index:%ld", (long)index);
        [sender setTitle:titles[index] forState:(UIControlStateNormal)];
        if (index == 0) {
            self.modelArray = [NSMutableArray array];
            self.idArray = [NSMutableArray array];
            
            [sender setTitle:@"个性" forState:(UIControlStateNormal)];
            self.personality = nil;
            [self setData];
            [self.collectionView reloadData];
            
        }else{
            self.modelArray = [NSMutableArray array];
            self.idArray = [NSMutableArray array];
            
            self.personality = self.personalityArray[index - 1];
            [self setData];
            [self.collectionView reloadData];
            
            
        }
        
    };
    [pop show];
}
-(void)button4Clicked:(UIButton *)sender
{
    
    
    CGPoint point = CGPointMake(sender.frame.origin.x + sender.frame.size.width/2, sender.frame.origin.y + sender.frame.size.height );
    NSArray *titles = @[@"全部", @"50以下", @"50-200",@"200-500",@"500-1000",@"1000以上"];
    NSArray *images = @[@"aa.gif", @"aa.gif", @"aa.gif", @"aa.gif", @"aa.gif", @"aa.gif"];
    
    PopoverView *pop = [[PopoverView alloc] initWithPoint:point titles:titles images:images];
    pop.selectRowAtIndex = ^(NSInteger index){
        NSLog(@"select index:%ld", (long)index);
        [sender setTitle:titles[index] forState:(UIControlStateNormal)];
        if (index == 0) {
            self.modelArray = [NSMutableArray array];
            self.idArray = [NSMutableArray array];
            [sender setTitle:@"价格" forState:(UIControlStateNormal)];
            self.price = nil;
            [self setData];
            [self.collectionView reloadData];
            
        }else{
            self.modelArray = [NSMutableArray array];
            self.idArray = [NSMutableArray array];
            self.price = self.priceArray[index - 1];
            [self setData];
            [self.collectionView reloadData];
        }
        
    };
    [pop show];
}

#pragma mark <UICollectionViewDelegate>


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WebViewController *webvc = [[WebViewController alloc]init];
    webvc.userid = self.idArray[indexPath.row];
    webvc.choosegift = self.modelArray[indexPath.row];
    [webvc setHidesBottomBarWhenPushed:YES];
    webvc.type = 1;

    [self.navigationController pushViewController:webvc animated:YES];
    



}

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
